
import { createRouter, RouteRecordRaw, RouteLocationNormalized, createWebHashHistory } from 'vue-router'
import { topRouter } from './vendors/topRouter'
import NProgress from 'nprogress'
import 'nprogress/css/nprogress.css' // 进度条样式
import useRoutersStore from './vendors/store/router'
import useTokenStore from '@/vendors/store/token'
import { message, MessageType } from '@/components/message/message'
import auth from './vendors/auth/auth'
import useUserInfo from '@/vendors/store/user'
import useMenuStore from '@/vendors/store/menu'

const home: Array<RouteRecordRaw> = [{
  path: '/',
  name: 'Home',
  component: () => import('@/components/layout/Home.vue'),
  redirect: {
    name:'CertificationList'
  },
  meta: {
    title: '首页',
    ignoredMenu: true
  }
}]

const layout = [
  {
    path: '/layout',
    name: 'layout',
    meta: {
      ignoredMenu: true
    },
    component: () => import('@/components/layout/Layout.vue'),
    children: home.concat(topRouter)
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/login/AdminLogin.vue'),
    meta: {
      title: 'login',
      ignoredMenu: true
    }
  },
  {
    path: '/register',
    component: () => import('@/components/login/Register.vue'),
    meta: {
      title: 'register',
      ignoredMenu: true
    }
  },
  {
    path: '/test',
    component: () => import('@/views/test.vue')
  }
]
NProgress.configure({ showSpinner: false })
const routes: Array<RouteRecordRaw> = layout

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((next: RouteLocationNormalized, from: RouteLocationNormalized) => {
  const tokenStore = useTokenStore()
  if (!tokenStore.getToken && next.path != '/login' && next.path != '/adminLogin' && next.path != '/register') {
    // 不存在token
    message('please login firstly', MessageType.error)
    router.replace({
      path: '/login'
    })
  }
  const userInfo = useUserInfo()
  // 鉴权判断是否符合权限
  if (next.meta.auth && userInfo.getUserInfo.admin == 0) {
    const authLocks = next.meta.auth as Array<string>
    authLocks.forEach((n: string) => {
      if (!auth[n] || !auth[n]()) {
        // 无权限
        throw Error('非法访问')
      }
    })
  }
  if (next.meta.runAuth && userInfo.getUserInfo.admin == 1) {
    const authLocks = next.meta.runAuth as Array<string>
    authLocks.forEach((n: string) => {
      if (!auth[n] || !auth[n]()) {
        // 无权限
        throw Error('非法访问')
      }
    })
  }
  if (next.meta && next.meta.bannedAdmins && (next.meta.bannedAdmins as Array<any>).findIndex((v: any) => v == userInfo.getUserInfo.admin) > -1) {
    // 无权限
    throw Error('非法访问')
  }
  const routerStore = useRoutersStore()
  routerStore.setCurRouter({ title: next.meta.title as string, path: next.path })
  NProgress.start()
  return true
})
router.afterEach((next, from) => {
  const menu = useMenuStore()
  menu.setBannedId(next.name as string)
  if (next.meta) {
    if (next.meta.title) {
      document.title = next.meta.title as string
    }
  }
  NProgress.done()
})
export default router
